<?php 
namespace Admin\Model;
use Think\Model;

class CategoryModel extends Model {

	protected $_validate = array(
		array('name','require','请填写分类名称！'),
		// array('name','','商品名称在3个字到50个字之间！',1,'length','3,30'),
	);

	//递归目录
	public function getCateByPid($pid=0){
        $result = $this->where("status=1 AND pid='{$pid}'")->select();
        if(!empty($result)){
            foreach ($result as $key => $value) {
                $result[$key]['child']=$this->getCateByPid($value['id']);
            }
        }
        return $result;
    }

    //递归获取select表单内容
    function getCateOptionHtml($selectId=0,$cate=array()){
        if(empty($cate)){
            $cate = $this->getCateByPid(0);
        }
        foreach ($cate as $key => $value) {
            if(is_array($selectId)){
                if(in_array($value['id'],$selectId)){
                    $this->optionStr = $this->optionStr.'<option value="'.$value['id'].'" selected="selected">';
                }else{
                    $this->optionStr = $this->optionStr.'<option value="'.$value['id'].'">';
                }
            }else{
                if($selectId==$value['id']){
                    $this->optionStr = $this->optionStr.'<option value="'.$value['id'].'" selected="selected">';
                }else{
                    $this->optionStr = $this->optionStr.'<option value="'.$value['id'].'">';
                }
            }
            
            for ($i=1; $i <$value['level'] ; $i++) { 
                $this->optionStr = $this->optionStr.'&nbsp;&nbsp;&nbsp;';
            }
            if($value['level']==1){
                $this->optionStr = $this->optionStr.$value['name'].'</opton>';
            }else{
                $this->optionStr = $this->optionStr.'└─'.$value['name'].'</opton>';
            }
            
            if(!empty($value['child'])){
                $this->getCateOptionHtml($selectId,$value['child']);
            }
            
        }
        return $this->optionStr;
    }

    //递归获取分类表格内容
    function getCateTableHtml($cate=array()){
        if(empty($cate)){
            $cateModel=D('Category');
            $cate = $cateModel->getCateByPid(0);
        }
        foreach ($cate as $key => $value) {
            $this->listStr = $this->listStr.'<tr><td>'.$value['id'].'</td><td>';
            for ($i=1; $i <$value['level'] ; $i++) { 
                $this->listStr = $this->listStr.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
            }
            if($value['level']==1){
                $this->listStr = $this->listStr.$value['name'].'</td>';
            }else{
                $this->listStr = $this->listStr.'└─'.$value['name'].'</td>';
            }
            $this->listStr = $this->listStr.'<td><a href="'.U('product/index').'/cid/'.$value['id'].'" class="btn btn-primary"><i class="fa fa-eye"></i></a</td>';
            $this->listStr = $this->listStr.'<td><a href="'.U('add').'/id/'.$value['id'].'" class="btn btn-primary"><i class="fa fa-edit"></i></a</td>';
            $this->listStr = $this->listStr.'<td><a href="'.U('del').'/id/'.$value['id'].'" class="btn btn-danger"><i class="fa fa-times"></i></a</td></tr>';
            if(!empty($value['child'])){
                $this->getCateTableHtml($value['child']);
            }
            
        }
        return $this->listStr;
    }
}